package com.foreveredu.propms.web.struts.action;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.propms.domain.ChargeItem;
import com.foreveredu.propms.domain.ChargeUnit;
import com.foreveredu.propms.domain.ChargeWay;
import com.foreveredu.propms.domain.SubDistrict;
import com.foreveredu.propms.service.IChargeService;
import com.foreveredu.propms.web.struts.actionform.ChargeItemActionForm;
import com.foreveredu.propms.web.struts.actionform.SubDistrictActionForm;

public class ChargeItemAction extends AbstractBaseDispatchAction {

	private IChargeService chargeService = null;

	public IChargeService getChargeService() {
		return chargeService;
	}
	
	public void setChargeService(IChargeService chargeService) {
		this.chargeService = chargeService;
	}

	// 跳转到收费项目列表页面
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得所有收费项目列表
		List<ChargeItem> itemList = chargeService.findChargeItems();
		request.setAttribute("itemList", itemList);
		
		return mapping.findForward("chargeItemList");
	}

	// 跳转到增加收费项目列表页面
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得收费单位列表
		List<ChargeUnit> cuList = chargeService.findChargeUnits();
		request.setAttribute("cuList", cuList);
		
		//获得收费方式列表
		List<ChargeWay> cwList = chargeService.findChargeWays();
		request.setAttribute("cwList", cwList);
		
		return mapping.findForward("addItem");
	}
	
	// 完成收费项目添加操作
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		//获得表单数据
		ChargeItemActionForm ciaf = (ChargeItemActionForm)form;
		
		// 表单校验、
		if (ciaf.getItemId() == null || "".equals(ciaf.getItemId())) {
			request.setAttribute("chargeItemIdError", "请输入收费项目编号！");

			return mapping.findForward("toAdd");
		} else if (chargeService.findChargeItemByItemId(ciaf.getItemId()) != null) {
			request.setAttribute("chargeItemIdRequest", "您输入的收费项目编号已存在，请重新输入！");

			return mapping.findForward("toAdd");
		}
		if(!Pattern.matches("^[0-9]+$", ciaf.getItemId())){
			request.setAttribute("chargeItemIdError", "收费项目编号编号必须为数字组成！");
			return mapping.findForward("toAdd");
		}
		if (ciaf.getName() == null || "".equals(ciaf.getName())) {
			request.setAttribute("chargeItemNameError", "请输入收费项目名称！");

			return mapping.findForward("toAdd");
		}
		if (ciaf.getChargeUnitId() == 0 ) {
			request.setAttribute("chargeItemUnitError", "请选择收费单位！");

			return mapping.findForward("toAdd");
		}
		if (ciaf.getChargeWayId() == 0 ) {
			request.setAttribute("chargeItemWayError", "请选择收费方式！");

			return mapping.findForward("toAdd");
		}
		if (ciaf.getRemark() == null || "".equals(ciaf.getRemark()) ) {
			request.setAttribute("chargeItemRemarkError", "请输入备注信息！");

			return mapping.findForward("toAdd");
		}
		
		
		//将表单数据复制到item对象中
		ChargeItem item = new ChargeItem();
		PropertyUtils.copyProperties(item, ciaf);
		
		//设置item对象的收费单位
		ChargeUnit unit = chargeService.findChargeUnitById(ciaf.getChargeUnitId());
		item.setChargeUnit(unit);
		
		//设置item对象的收费方式
		ChargeWay way = chargeService.findChargeWayById(ciaf.getChargeWayId());
		item.setChargeWay(way);
		
		//保存item对象
		chargeService.saveChargeItem(item);
		
		return mapping.findForward("toChargeItemList");
	}

	// 跳转到收费项目修改页面
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ChargeItemActionForm ciaf = (ChargeItemActionForm)form;
		ChargeItem chargeItem = chargeService.findChargeItemById(ciaf.getId());
		request.setAttribute("chargeItem", chargeItem);

		//获得收费单位列表
		List<ChargeUnit> cuList = chargeService.findChargeUnits();
		request.setAttribute("cuList", cuList);
		
		//获得收费方式列表
		List<ChargeWay> cwList = chargeService.findChargeWays();
		request.setAttribute("cwList", cwList);

		return mapping.findForward("chargeItemUpdate");
	}

	// 保存收费项目修改数据
	public ActionForward updateSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ChargeItemActionForm ciaf = (ChargeItemActionForm)form;
		
		// 表单校验、
	if (!ciaf.getItemId().equals(chargeService.findChargeItemById(ciaf.getId())
				.getItemId())) {// 校验提交的小区编码是否修改
		if (ciaf.getItemId() == null || "".equals(ciaf.getItemId())) {
			request.setAttribute("chargeItemIdError", "请输入收费项目编号！");

			return mapping.findForward("toUpdate");
		} else if (chargeService.findChargeItemByItemId(ciaf.getItemId()) != null) {
			request.setAttribute("chargeItemIdRequest", "您输入的收费项目编号已存在，请重新输入！");

			return mapping.findForward("toUpdate");
		}
		if(!Pattern.matches("^[0-9]+$", ciaf.getItemId())){
			request.setAttribute("chargeItemIdError", "收费项目编号编号必须为数字组成！");
			return mapping.findForward("toUpdate");
		}
	}
		if (ciaf.getName() == null || "".equals(ciaf.getName())) {
			request.setAttribute("chargeItemNameError", "请输入收费项目名称！");

			return mapping.findForward("toUpdate");
		}
		if (ciaf.getChargeUnitId() == 0 ) {
			request.setAttribute("chargeItemUnitError", "请选择收费单位！");

			return mapping.findForward("toUpdate");
		}
		if (ciaf.getChargeWayId() == 0 ) {
			request.setAttribute("chargeItemWayError", "请选择收费方式！");

			return mapping.findForward("toUpdate");
		}
		if (ciaf.getRemark() == null || "".equals(ciaf.getRemark()) ) {
			request.setAttribute("chargeItemRemarkError", "请输入备注信息！");

			return mapping.findForward("toUpdate");
		}
		
		ChargeItem chargeItem =chargeService.findChargeItemById(ciaf.getId());
		PropertyUtils.copyProperties(chargeItem, ciaf);
		
		ChargeUnit chargeUnit = chargeService.findChargeUnitById(ciaf.getChargeUnitId());
		chargeItem.setChargeUnit(chargeUnit);
		
		ChargeWay chargeWay = chargeService.findChargeWayById(ciaf.getChargeWayId());
		chargeItem.setChargeWay(chargeWay);
		
		chargeService.updateChargeItem(chargeItem);
		
		return mapping.findForward("toChargeItemList");
	}

	// 完成收费项目删除操作
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ChargeItemActionForm ciaf = (ChargeItemActionForm)form;
		chargeService.deleteChargeItemById(ciaf.getId());

		//获得所有收费项目列表
		List<ChargeItem> itemList = chargeService.findChargeItems();
		request.setAttribute("itemList", itemList);
		
		return mapping.findForward("toChargeItemList");
	}

}
